package com.sgxy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sgxy.entity.EmployeeInf;
import com.sgxy.entity.DeptInf;
import com.sgxy.service.EmployeeInfService;
import com.sgxy.service.DeptInfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/dept")
@Controller
public class DeptInfController {

    private final DeptInfService deptInfService;
    @Autowired
    public DeptInfController(DeptInfService deptInfService) {
        this.deptInfService = deptInfService;
    }

    @RequestMapping("/selectdept")
    public ModelAndView selectDeptInfAction(DeptInf deptInf,
                                           @RequestParam(value = "page", defaultValue = "1")Integer page){
        ModelAndView modelAndView = new ModelAndView();
        PageHelper.startPage(page,10);
        List<DeptInf> deptInfs = deptInfService.queryAllDeptInfs(deptInf);
        PageInfo<DeptInf> deptInfPageInfo = new PageInfo<>(deptInfs);
        modelAndView.addObject("deptInfPageInfo",deptInfPageInfo);
        modelAndView.addObject("deptInf",deptInf);
        modelAndView.setViewName("/dept/dept.jsp");
        return modelAndView;
    }

    @RequestMapping("/toAdddept")
    public ModelAndView toAdddeptAction(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/dept/showAddDept.jsp");
        return modelAndView;
    }

    @RequestMapping("/adddept")
    public ModelAndView addDeptAction(DeptInf deptInf){
        ModelAndView modelAndView  = new ModelAndView();
        int i = deptInfService.addDeptInf(deptInf);
//        if(i>0){
//            modelAndView.setViewName("employee.jsp");
//        }
        modelAndView.setViewName("redirect:/dept/toAdddept");
        return modelAndView;
    }
    @RequestMapping("/updatedept")
    public ModelAndView updateDeptAction(DeptInf deptInf){
        ModelAndView modelAndView = new ModelAndView();
        int i = deptInfService.updateDeptInf(deptInf);
        modelAndView.setViewName("redirect:/dept/selectdept");
        return modelAndView;
    }
    //返回值可能会有多个
    @RequestMapping("/deletedept")
    public ModelAndView deleteDeptAction(String ids){
        ModelAndView modelAndView = new ModelAndView();
        String[] split = ids.split(",");
        List<Integer> list = new ArrayList<Integer>();
        for(int i=0;i<split.length;i++){
            list.add(Integer.valueOf(split[i]));
        }
        int i = deptInfService.deleteDeptInf(list);
        modelAndView.setViewName("redirect:/dept/selectdept");
        return modelAndView;
    }

    @RequestMapping("/toUpdatedept")
    public ModelAndView toUpdateDeptAction(Integer id){
        ModelAndView modelAndView = new ModelAndView();
        DeptInf deptInf = deptInfService.queryDeptInf(id);
        modelAndView.addObject("deptInf",deptInf);
        modelAndView.setViewName("/dept/showUpdateDept.jsp");
        return modelAndView;
    }
}
